import cv2
import os
import numpy as np

# 图片保存路径
IMG_SAVE_PATH = "img"


if __name__ == '__main__':
    print(f"will save {IMG_SAVE_PATH}")
    os.makedirs(IMG_SAVE_PATH,exist_ok=True)
    num = 1
    camera = cv2.VideoCapture("http://192.168.1.9/api/stream")
    while True:
        state, src = camera.read()
        '''
        gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
        # 二维码检测器
        qrcoder = cv2.QRCodeDetector()
        # 检测识别二维码
        codeinfo, points, straight_qrcode = qrcoder.detectAndDecode(gray)
        qrcode_win = np.copy(src)
        if points is not None:
            cv2.drawContours(qrcode_win, [np.int32(points)], 0, (0, 0, 255), 2)
        # 输出识别二维码的信息
        print("qrcode information is : \n%s"% codeinfo)
        cv2.imshow("qrcode",qrcode_win)
        '''
        # 空格保存图片
        cv2.imshow('win', src)
        if cv2.waitKey(2) & 0xff == ord(' '):
            cv2.imwrite(os.path.join(IMG_SAVE_PATH,f"{str(num)}.jpg"), src)
            print("Saved img_" + str(num) + "!")
            num += 1
        if cv2.waitKey(2) & 0xff == ord('q'):
            break
    cv2.destroyAllWindows()
